<?php
/**
 * Created by PhpStorm.
 * Author: MeiJay
 * Date: 2018/11/3
 * Time: 15:04
 */

namespace APP\Http\Controllers\Api\V1;


use App\Http\Controllers\Api\BaseController;
use App\Model\Member;
use App\Service\Activity\ActivityService;
use App\Service\FileService;
use App\Service\WechatService;
use Illuminate\Http\Request;
use Upyun\Config;
use Upyun\Signature;
use Upyun\Util;
use Validator;

class IndexController extends BaseController
{

    /**
     * 登陆接口
     * @param Request $request
     * @author 张永辉 2018年11月3日
     */
    public function login(Request $request, WechatService $wechatService)
    {
        $input = $request->input();
        $rule  = [
            'code' => 'required',
        ];

        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            return error($validator->errors()->first());
        }
        $result = $wechatService->setParams($input)->login();
        $result['errCode'] && exit(error($result['errMsg']));
        return success($result['data']);
    }


    /**
     * 服务授权
     * @author 张永辉 2018年11月5日
     */
    public function serviceAuthorization(Request $request, WechatService $wechatService)
    {
        $mid      = $request->input('mid');
        $userInfo = $request->input('userInfo', '');
        !$userInfo && error('数据不能为空');
        $data   = [
            'nickname'   => $userInfo['nickName'],
            'headimgurl' => $userInfo['avatarUrl'],
            'sex'        => $userInfo['gender'],
            'country'    => $userInfo['country'],
            'province'   => $userInfo['province'],
            'city'       => $userInfo['city'],
        ];
        $result = $wechatService->setParams(['id' => $mid, 'data' => $data])->updataMember();
        $result['errCode'] && exit(error($result['errMsg']));
        success();
    }


    /**
     * 是否过期
     * @author 张永辉 2018年11月5日
     */
    public function isTokenExpire(Request $request, WechatService $wechatService)
    {
        $input     = $request->input();
        $rule      = [
            'token' => 'required',
        ];
        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            return error($validator->errors()->first());
        }

        $data['isExpire'] = $wechatService->setParams(['token' => $input['token']])->isTokenExpire();
        return success($data);
    }


    /**
     * 获取cdn信息
     * @author 张永辉 2018年11月5日
     */
    public function getCDNInfo(Request $request, FileService $fileService)
    {
        $bucket = config('app.cdn_bucket');
        $config = new Config($bucket, 'phpteam', 'phpteam123456');
        $config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw=');
        $data['save-key']             = $fileService->getPath();
        $data['expiration']           = time() + 120;
        $data['bucket']               = $bucket;
        $data['notify-url']           = 'https://hsshop.huisou.cn/merchants/myfile/notify/';
        $data['content-length-range'] = '0,10240000';
        $policy                       = Util::base64Json($data);
        $method                       = 'POST';
        $uri                          = '/' . $bucket;
        $signature                    = Signature::getBodySignature($config, $method, $uri, null, $policy);
        $data                         = [
            'policy'        => $policy,
            'authorization' => $signature,
            'url'           => 'https://v0.api.upyun.com/' . $bucket,
        ];
        success($data);
    }


    /**
     * 设置图片
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|mixed|string|void
     * @author 张永辉 2018年11月6日
     */
    public function setFile(Request $request, FileService $fileService)
    {
        $input     = $request->input();
        $rule      = [
            'cdn_path'  => 'required',
            'type'      => 'required',
            'img_size'  => 'required',
            'file_mine' => 'required',
            'size'      => 'required',
        ];
        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            return error($validator->errors()->first());
        }

        $data = $fileService->setParams($input)->setFile();
        success($data);
    }


	/**
     * 首页
     * 获取所有的活动信息
     * @author: 梅杰
     */
    public function index(ActivityService $activityService)
    {
        $data = $activityService->getPage();
        success($data);
    }


}